<!--
 * @Author: Liuxin 2849181826@qq.com
 * @Date: 2025-04-08 12:21:01
 * @LastEditors: Liuxin 2849181826@qq.com
 * @LastEditTime: 2025-04-09 10:22:28
 * @FilePath: \node-exe\electron-vite-vue\src\App.vue
 * @Description: 应用入口组件
-->
<script setup lang="ts">
import { onMounted, onUnmounted } from "vue"
// 修改导入方式，使用默认导入
import ErrorNotification from './components/ErrorNotification.vue'
import { useErrorStore } from './store/modules/error'

// 初始化全局错误处理
const errorStore = useErrorStore()

// 设置全局未捕获异常处理器
function setupGlobalErrorHandlers() {
  const handleError = (event: ErrorEvent) => {
    event.preventDefault()
    errorStore.reportError({
      type: 'rendererError',
      message: event.message || 'Unknown error',
      stack: event.error?.stack,
      source: 'renderer'
    })
  }

  const handleUnhandledRejection = (event: PromiseRejectionEvent) => {
    event.preventDefault()
    errorStore.reportError({
      type: 'rendererUnhandledRejection',
      message: event.reason?.message || String(event.reason) || 'Unhandled promise rejection',
      stack: event.reason?.stack,
      source: 'renderer'
    })
  }

  // 添加全局错误处理器
  window.addEventListener('error', handleError)
  window.addEventListener('unhandledrejection', handleUnhandledRejection)

  // 移除处理器函数
  return () => {
    window.removeEventListener('error', handleError)
    window.removeEventListener('unhandledrejection', handleUnhandledRejection)
  }
}

onMounted(() => {
  // 设置全局错误处理
  const removeHandlers = setupGlobalErrorHandlers()

  // 组件卸载时移除全局处理器
  onUnmounted(() => {
    removeHandlers()
  })
})
</script>

<template>
  <div class="app-container">
    <router-view />
    <!-- 添加错误通知组件 -->
    <ErrorNotification />
  </div>
</template>

<style lang="less">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100%;
}

.app-container {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>
